package com.xkdx.caipiao.wiget;

import java.util.ArrayList;
import java.util.HashSet;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.hall.CaiPiaoActivity;
import com.xkdx.caipiao.hall.LottoActivity;
import com.xkdx.caipiao.listener.OnBallClickListener;

@SuppressLint("ResourceAsColor")
public class BlueBallButton extends Button {
	Context context;
	String text;
	boolean isSelected = false;
	String posi = "wan";
	OnBallClickListener listener;
	String mark = "";
	View self;

	public BlueBallButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		this.context = context;
		init();

	}

	public BlueBallButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public BlueBallButton(Context context) {
		super(context);
		this.context = context;
		init();
	}

	public void setPosi(String posi) {
		this.posi = posi;
	}

	public void setBallClickListener(OnBallClickListener l) {
		this.listener = l;
	}

	public boolean getState() {
		return isSelected;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public String getMark() {
		return mark;
	}

	public void setLottoState(HashSet<Integer> set) {

		if (set.contains(Integer.valueOf(text))) {
			isSelected = true;
			setBackgroundResource(R.drawable.ball_blue_caipiao);
			setTextColor(getResources().getColor(R.color.white_caipiao));
		} else {
			isSelected = false;
			setBackgroundResource(R.drawable.ball_gray_caipiao);
			setTextColor(getResources().getColor(R.color.blue_caipiao));
		}
		invalidate();
	}

	public void setState(ArrayList<String> set) {
		if (set.contains(text)) {
			isSelected = true;
			setBackgroundResource(R.drawable.ball_blue_caipiao);
			setTextColor(getResources().getColor(R.color.white_caipiao));
		} else {
			isSelected = false;
			setBackgroundResource(R.drawable.ball_gray_caipiao);
			setTextColor(getResources().getColor(R.color.blue_caipiao));
		}
		
	}

	public void setSelected() {
		isSelected = true;
		setBackgroundResource(R.drawable.ball_blue_caipiao);
		setTextColor(getResources().getColor(R.color.blue_caipiao));
		
	}

	@SuppressLint("ResourceAsColor")
	private void init() {
		self = this;
		setBackgroundResource(R.drawable.ball_blue_caipiao);
		setTextColor(getResources().getColor(R.color.blue_caipiao));
		this.post(new Runnable() {

			@Override
			public void run() {
				text = getText().toString();
				if (text.equals("00")) {
					isSelected = true;
					setBackgroundResource(R.drawable.ball_blue_caipiao);
					setTextColor(getResources().getColor(R.color.white_caipiao));
				} else {
					isSelected = false;
					setBackgroundResource(R.drawable.ball_gray_caipiao);
					setTextColor(getResources().getColor(R.color.blue_caipiao));
				}
			}
		});

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if (isSelected) {
				isSelected = !isSelected;
				setBackgroundResource(R.drawable.ball_gray_caipiao);
				setTextColor(getResources().getColor(R.color.blue_caipiao));
				listener.RemoveBall(mark, text);

			} else {
				isSelected = !isSelected;
				setBackgroundResource(R.drawable.ball_blue_caipiao);
				setTextColor(getResources().getColor(R.color.white_caipiao));
				listener.AddBall(mark, text);

			}
			listener.DismissBallPopowindow();
			((CaiPiaoActivity) context).setMutiplyList();

		} else if (event.getAction() == MotionEvent.ACTION_DOWN) {

//			listener.ShowBallPopoWidow(self, text);
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
//			if (event.getX() > getRight() || event.getX() < getLeft()
//					|| event.getY() < getTop() || event.getY() > getBottom()) {

				listener.DismissBallPopowindow();
//			}
		}

		return true;

	}

}
